<?php

namespace App\Models\Api;
use Illuminate\Database\Eloquent\Model;
use App\Models\Api\Base;
use App\Models\Api\Recode;
use Cache;
class AwardUser extends Base
{
    protected $table = 'award_user';
    public $timestamps = false;
    /**
     * 操作时间
     * @return string
     */
    protected function getFortuneTimeAttr($value, $data)
    {
        return date('Y-m-d', json_decode($data['extend'], true)['award_time']);
    }

    /**获取档案所欲中奖的运势
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     * @throws \app\common\lib\exception\ApiException
     */
    public function get_award_fortune($map = [])
    {
        $uid = $map['uid'] ?? 0;
        $rid = $map['rid'] ?? 0;
        $type = $map['type'] ?? 0;
        $self_id = $map['self_id'] ?? 0;

        $query = $this
            ->from('award_user as au')
            ->select('au.status','au.extend','g.expire_time','au.aid','au.id','au.rid')
            ->join('goods as g', 'au.aid', '=', 'g.id');

switch ($type) {
    case 1:
        $query->whereIn('au.aid', config('app.day_id'));
        break;
    case 3:
        $query->whereIn('au.aid', config('app.month_id'));
        break;
    case 4:
        $query->whereIn('au.aid', config('app.year_id'));
        break;
}

$query->where('au.uid', $uid);

if (is_array($self_id) && isset($self_id['id']) && $rid == $self_id['id']) {

    $query->where(function ($q) use ($self_id) {
        $q->where('au.rid', 0)
          ->orWhere('au.rid', $self_id['id']);
    });
} else {
    $query->where('au.rid', $rid);
}

        $prize = false;

        // if ($this->cache_on >= 1) {
        //     $prize = Cache::get($uid . '_' . $rid . '_award_fortune_' . $type);
        // }

        if ($prize === false) {
            // 获取中奖标识
            try {

                // 获取中奖标识
                // $prize = $this
                //         ->from('award_user as au')
                //         ->select('au.status','au.extend','g.expire_time','au.aid','au.id','au.rid')
                //         ->join('goods as g','au.aid','=','g.id','INNER')
                //         ->where($where)
                //         ->get();
                 $prize = $query->get()->toArray();
               // var_dump($prize);die;
                $prize = json_decode(json_encode($prize, true), true);
                
                if ($this->cache_on >= 1) {
                    Cache::set($uid . '_' . $rid . '_award_fortune_' . $type, $prize, 86400 * 7);
                }

            } catch (\Exception $e) {
                $prize = [];
            }
            //更新用户赠送的rid，0为自己的档案id
            if ($prize) {
                foreach ($prize as $v) {
                    if (!$v['rid'] && $self_id) {
                        $this->where('id', $v['id'])->update(['rid' => $self_id['id']]);
                    }
                }
            }
        }
        return $prize;
    }


    /**
     *根据id获取中奖的相关信息
     */
    public function getAwardForId($mid = 0)
    {
        $data = false;
        if ($this->cache_on >= 1)
            $data = Cache::get('award_fortune_' . $mid);

        if ($data == false) {
            try {
                $data = $this
                    ->from('award_user as au')
                    ->select('au.create_time','au.extend','au.status','u.username','u.nickname','u.sex','u.uen','u.id as uid','u.tel','g.expire_time','au.rid')
                    ->join('goods as g', 'au.aid','=','g.id', 'LEFT')
                    ->join('user as u', 'au.uid', '=','u.id', 'LEFT')
                    ->where('au.id', $mid)
                    ->firstOrFail();
            } catch (\Exception $e) {
                $data = [];
            }

            if ($this->cache_on >= 1 && $data)
                Cache::set('award_frotune_' . $mid, $data, 86400 * 7);
        }
    }


}
